//
// Created on 2025-08-11.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "json_stringify.h"
#include "yyjson.h"
#include "yyjson_util.h"

napi_value json_stringify::Stringify(napi_env env, napi_value value) {
    unique_yyjson_mut_doc doc(yyjson_mut_doc_new(nullptr));
    yyjson_mut_val *root = napi_to_yyjson(env, doc.get(), value);
    yyjson_mut_doc_set_root(doc.get(), root);
    
    const char *json = yyjson_mut_write(doc.get(), YYJSON_WRITE_NOFLAG, nullptr);
    
    napi_value result;
    if (json) {
        napi_create_string_utf8(env, json, NAPI_AUTO_LENGTH, &result);
        free((void*)json);
    } else {
        napi_create_string_utf8(env, "null", NAPI_AUTO_LENGTH, &result);
    }
    return result;
}